package com.wish.group05.financial.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wish.group05.base.dto.SaveJrFactoringDto;
import com.wish.group05.base.entity.CfContract;
import com.wish.group05.base.entity.JrFactoringDetail;
import com.wish.group05.base.entity.JrProduct;
import com.wish.group05.base.service.ICfContractService;
import com.wish.group05.base.service.IJrFactoringDetailService;
import com.wish.group05.base.service.IJrFactoringService;
import com.wish.group05.base.service.IJrProductService;
import com.wish.group05.base.vo.CatulateFactorInfoVo;
import com.wish.group05.base.vo.FinancingVo;
import com.wish.group05.common.core.web.controller.BaseController;
import com.wish.group05.common.core.web.domain.AjaxResult;
import com.wish.group05.common.core.web.page.TableDataInfo;
import com.wish.group05.financial.service.FinancialVoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("factoring")
@RestController
public class ApplyforfactoringController extends BaseController {

    @Autowired
    private IJrProductService iJrProductService;

    @Autowired
    ICfContractService iCfContractService;

    @Autowired
    IJrFactoringDetailService iJrFactoringDetailService;

    @Autowired
    IJrFactoringService iJrFactoringService;


    @GetMapping("financialList")
    public TableDataInfo financialList(JrProduct jrProduct)
    {
        List<JrProduct> list = iJrProductService.list(new QueryWrapper<JrProduct>().eq("jr_product_type", 3).eq("auditing", 1).eq("del_flg", 0));


        return getDataTable(list);
    }

    //查询租房合同信息
    @RequestMapping("contractList")
    public AjaxResult contractList(CfContract cfContract, @RequestParam(defaultValue = "1")int pageNum, @RequestParam(defaultValue = "4")int pageSize){

        PageHelper.startPage(pageNum,pageSize);

        QueryWrapper wrapper = new QueryWrapper<>();
        //未申请过租房分期
        wrapper.eq("rent_installment_flg",0);
        //未申请过租约保理
        wrapper.eq("zybl_flg",0);
        //合同状态是正常执行中状态
        wrapper.eq("contract_state",2);
        //未续约
        wrapper.eq("xuyue_state",0);
        //未冻结状态
        wrapper.eq("freeze_status",0);
        //未删除
        wrapper.eq("del_flg",0);
        if(cfContract.getPremiseName()!=null&&!"".equals(cfContract.getPremiseName())){
            wrapper.like("premise_name",cfContract.getPremiseName());
        }
        if(cfContract.getTenantName()!=null&&!"".equals(cfContract.getTenantName())){
            wrapper.like("tenant_name",cfContract.getTenantName());
        }
        List<CfContract> list = iCfContractService.list(wrapper);
        PageInfo<CfContract> cfContractPageInfo = new PageInfo<>(list);
        return AjaxResult.success(cfContractPageInfo);
    }

    @GetMapping("catulateFactorInfo")
    public AjaxResult catulateFactorInfo(CatulateFactorInfoVo catulateFactorInfoVo){
        JrFactoringDetail jrFactoringDetail = iJrFactoringDetailService.catulateFactorInfo(catulateFactorInfoVo);
        System.err.println(jrFactoringDetail);
        return AjaxResult.success(jrFactoringDetail);
    }

    @PostMapping
    public AjaxResult saveFactoringApply(@RequestBody SaveJrFactoringDto saveJrFactoringDto){
        System.err.println(saveJrFactoringDto);
        Integer integer = iJrFactoringService.saveFactoringApply(saveJrFactoringDto);
        if(integer==1){
            return AjaxResult.success("未授权");
        }
        if(integer==2){
            return AjaxResult.success("授信额度不足,请联系维数公寓客服:400-008-3363");
        }

    return AjaxResult.success("操作成功");
    }


}
